package com.jt.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created with IntelliJ IDEA.
 * User: zhaoqiubo
 * Date: 15/7/3
 * Time: 上午10:58
 * 时间工具类
 */
public class GlobalTime {

    private static long timeMillisOffset;

    private GlobalTime() {
    }

    public static void setTimeMillisOffset(long timeMillisOffset) {
        timeMillisOffset = timeMillisOffset;
    }

    public static void synchronizeGlobalTimeMillis() {
    }

    public static final long globalTimeMillis() {
        return System.currentTimeMillis() + timeMillisOffset;
    }

    public static final long globalTimeUtc() {
        return globalTimeMillis() / 1000L;
    }

    /**
     * @return
     */
    public static final Date globalTime() {
        return new Date(globalTimeMillis());
    }

    /**
     * 返回当前系统时间毫秒数
     *
     * @return
     */
    public static final long systemTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 返回当前系统时间的Date对象
     *
     * @return
     */
    public static final Date systemTime() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 返回当前系统时间UTC秒
     *
     * @return
     */
    public static final int systemTimeUtc() {
        return (int) (systemTimeMillis() / 1000L);
    }

    public static String utc2Local(String utcTime, String utcTimePatten,
                                   String localTimePatten) {
        SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
        utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date gpsUTCDate = null;
        try {
            gpsUTCDate = utcFormater.parse(utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat localFormater = new SimpleDateFormat(localTimePatten);
        localFormater.setTimeZone(TimeZone.getDefault());
        String localTime = localFormater.format(gpsUTCDate.getTime());
        return localTime;
    }


    /**
     * 返回utc秒数所代表的"Asia/Shanghai"时区的当前年
     *
     * @param utc
     * @return
     */
    public static String utc2year(Long utc) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(utc * 1000);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return Integer.toString(c.get(Calendar.YEAR));
    }

    /**
     * 返回utc秒数所代表的"Asia/Shanghai"时区当年第几个月
     *
     * @param utc
     * @return
     */
    public static String utc2Month(long utc) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(utc * 1000);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return Integer.toString(c.get(Calendar.MONTH) + 1);

    }

    /**
     * 返回utc秒数所代表的"Asia/Shanghai"时区当天的第几个小时
     *
     * @param utc
     * @return
     */
    public static String utc2hourOfDay(long utc) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(utc * 1000);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return Integer.toString(c.get(Calendar.HOUR_OF_DAY));
    }

    /**
     * 返回utc秒数所代表的"Asia/Shanghai"时区当前年的第几周
     *
     * @param utc
     * @return
     */
    public static String utc2WeekOfYear(long utc) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(utc * 1000);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
    }

    /**
     * 返回utc秒数所代表的“Asia/Shanghai”时区的当月的第几周
     *
     * @param utc
     * @return
     */
    public static String utc2WeekOfMonth(long utc) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(utc * 1000);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return Integer.toString(c.get(Calendar.WEEK_OF_MONTH));
    }

    /**
     * 放回utc秒数所代表的“Asia/Shanghai”时区的当月的第几天
     *
     * @param utc
     * @return
     */
    public static String utc2DayOfMonth(long utc) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(utc * 1000);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return Integer.toString(c.get(Calendar.DAY_OF_MONTH));
    }

  
}
